//
//  DCTabbarViewController.m
//  Demo
//
//  Created by Kyle on 2022/12/29.
//

#import "DCTabbarViewController.h"
#import "DCBaseNavController.h"
#import "DCHomeViewController.h"
#import "DCLoanListController.h"
#import "DCMineViewController.h"
#import "DCHomeDefaultViewController.h"
#import "DCLoginViewController.h"
@interface DCTabbarViewController ()

@end

@implementation DCTabbarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureUI];
    [self getDataFromNet];
    // Do any additional setup after loading the view.
}
- (void)configureUI {
    UIImage *tabbarImage = [UIImage imageWithColor:[UIColor colorWithString:@"#FFFFFF"]];
    self.tabBar.backgroundImage = tabbarImage;
    self.tabBar.shadowImage = tabbarImage;
}

- (void)configureControllers:(BOOL)isDefault {
    DCBaseViewController *homeVC;
    if (isDefault) {
        homeVC = [DCHomeDefaultViewController new];
    } else {
        homeVC = [[DCHomeViewController alloc] init];
    }
    DCBaseNavController *homeNav = [[DCBaseNavController alloc] initWithRootViewController:homeVC];
    
    DCLoanListController *listVC = [[DCLoanListController alloc] init];
    DCBaseNavController *listNav = [[DCBaseNavController alloc] initWithRootViewController:listVC];
    
    DCMineViewController *mineVC = [[DCMineViewController alloc] init];
    DCBaseNavController *mineNav = [[DCBaseNavController alloc] initWithRootViewController:mineVC];
    
    NSArray *controllers = @[homeNav,listNav,mineNav];
    self.viewControllers = controllers;
    
    
    NSArray *selectArr = @[@"homeSelect", @"loanSelect", @"mineSelect"];
    NSArray *notSelectArr = @[@"homeNotSelect", @"loanNotSelect", @"mineNotSelect"];
    for (int i = 0; i < self.viewControllers.count; i++) {
        UITabBarItem *item = self.tabBar.items[i];
        item.selectedImage = [[UIImage imageNamed:selectArr[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        item.image = [[UIImage imageNamed:notSelectArr[i]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    }
//    for (UITabBarItem *item in self.tabBar.items) {
//
//    }
//    UITabBarItem *homeItem = [[UITabBarItem alloc] initWithTitle:@"1" image:[UIImage imageNamed:@"loanNotSelect"] selectedImage:[UIImage imageNamed:@"loanSelect"]];
//    UITabBarItem *listItem = [[UITabBarItem alloc] initWithTitle:@"2" image:[UIImage imageNamed:@"loanNotSelect"] selectedImage:[UIImage imageNamed:@"loanSelect"]];
//    UITabBarItem *mineItem = [[UITabBarItem alloc] initWithTitle:@"3" image:[UIImage imageNamed:@"mineNotSelect"] selectedImage:[UIImage imageNamed:@"mineSelect"]];
//    NSArray *tabarItems = @[homeItem, listItem, mineItem];
//    self.tabBar.items = tabarItems;
//
    self.selectedIndex = 0;
    
    
}

- (void)getDataFromNet {
    [DCHttpRequest DCAuthNextStepCompleteBlock:^(NSURLSessionDataTask * _Nonnull operation, DCResponseData * _Nonnull responseObject) {
        if (responseObject.code == 200) {
            if ([responseObject.data isKindOfClass:[NSString class]]) {
                NSString *step = (NSString *)responseObject.data;
                BOOL isDefault = [step isEqualToString:@"100"];
                [self configureControllers:isDefault];
            }
        } else {
            [DCDCToast toastText:responseObject.message];
            DCLoginViewController *loginVC = [DCLoginViewController new];
            DCBaseNavController *nav = [[DCBaseNavController alloc] initWithRootViewController:loginVC];
            UIWindow *keyWindow = APPDelegate.window;
            keyWindow.rootViewController = nav;
        }
    }];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
